/**
 * @Description: redis数据库
 * @Version: 1.0
 * @Author: 琴时
 * @Date: 2021-12-06 16:37:56
 * @LastEditors: 琴时
 * @LastEditTime: 2021-12-30 09:40:36
 */

import Redis from 'ioredis'
import config from '../../config'
const { redis_config } = config
const redisKeyPrefix = 'piante:redis:key:'
class AccessTokenDao {
  /* 创建连接 */
  createRedisConnection() {
    return new Redis(redis_config)
  }
  /* 存储信息 */
  async setData(key, value) {
    const redis = this.createRedisConnection()
    await redis.set(redisKeyPrefix + key, value, 'ex', 7200, (err, res) => {
      redis.quit()
      if (err) return err
      return res
    })
  }

  /* 获取信息 */
  async getData(key) {
    const redis = this.createRedisConnection()
    redis.get(redisKeyPrefix + key, (err, res) => {
      redis.quit()
      console.log('测试res:', res)
      if (err) return err
      
      return res
    })
  }

  /* 重置过期时间 */
  async resetExpirationTime(key, expire) {
    const redis = this.createRedisConnection()
    redis.expire(redisKeyPrefix + key, expire, (err, res) => {
      redis.quit()
      if (err) return err
      return res
    })
  }

  /* 删除数据 */
  async removeData(key) {
    const redis = this.createRedisConnection()
    redis.del(redisKeyPrefix + key, 7200, (err, res) => {
      redis.quit()
      if (err) return err
      return res
    })
  }
}

export default new AccessTokenDao()
